<template>
  <h3>点检保养项目</h3>
  <!-- 搜索栏 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-form :model="scmiQuery" inline>
        <el-form-item label="项目编码">
          <el-input v-model="scmiQuery.scmiEncode" placeholder="请输入项目编码"/>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="scmiQuery.scmiName" placeholder="请输入项目名称"/>
        </el-form-item>
        <el-form-item label="项目类型">
          <el-select placeholder="全部" style="width:100px" v-model="scmiQuery.scmiType">
            <el-option label="全部" :value="0"/>
            <el-option label="设备点检" :value="1"/>
            <el-option label="设备保养" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadScmis">搜索</el-button>
          <el-button @click="reloadScmis">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
  <!-- 工具栏 -->
  <el-row>
    <el-col :span="24">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-button type="primary" @click="toEdits">修改</el-button>
      <el-button type="primary" @click="toDelete">删除</el-button>
    </el-col>
  </el-row>
  <!-- 数据表 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-table
          :data="scmis"
          border
          stripe
      ><!-- stripe可以为表格添加斑马纹样式，默认false -->
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="scmiEncode" label="项目编码" width="150" align="center"/>
        <el-table-column prop="scmiName" label="项目名称" width="150" align="center"/>
        <el-table-column prop="scmiType" label="项目类型" width="150" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.scmiType === 1">设备点检</el-tag>
            <el-tag v-else>设备保养</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="scmiContent" label="项目内容" width="150" align="center"/>
        <el-table-column prop="scmiStandard" label="标准" width="150" align="center"/>
        <el-table-column prop="scmiStatus" label="是否启用" width="150" align="center">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.scmiStatus === 1">是</el-tag>
            <el-tag type="danger" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="scmiRemark" label="备注" width="200" align="center"/>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button-group>
              <el-button size="small" type="primary" @click="editScmi(scope.row.scmiId)">修改</el-button>
              <el-button size="small" type="danger" @click="deleteScmi(scope.row)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
  <!-- 新增点检保养项目弹窗 -->
  <maintain-items-add :flag="addScmi" @cancelAdd="cancelAdd" @addComplete="loadScmis"/>
  <!-- 编辑点检保养项目弹窗 -->
  <maintain-items-edit :scmiId="editScmiId" @cancelEdit="cancelEdit" @editComplete="loadScmis"/>
</template>

<script setup>

// 封装查询条件
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import scmiRequest from "@/api/scmi.js";
import MaintainItemsAdd from "@/views/device/maintainItems-add.vue";
import MaintainItemsEdit from "@/views/device/maintainItems-edit.vue";

// 封装查询条件
let scmiQuery = ref({});
// 封装查询的结果集合
let scmis = ref([]);
// 新增的addScmi
let addScmi = ref(-1);
// 编辑的editScmiId
let editScmiId = ref(-1);


function toAdd(){
  addScmi.value = 1;
}
function cancelAdd(){
  addScmi.value = -1;
}
function editScmi(scmiId){
  editScmiId.value = scmiId;
}
function cancelEdit() {
  editScmiId.value = -1;
}
function loadScmis(){
  scmiRequest.loadScmis(scmiQuery.value,res=>{
    scmis.value = res.data;
  })
}

function reloadScmis(){
  scmiQuery.value.scmiEncode = null;
  scmiQuery.value.scmiName = null;
  scmiQuery.value.scmiType = 0;
  scmiRequest.loadScmis(scmiQuery.value,res=>{
    scmis.value = res.data;
  })
}
function deleteScmi(scmi){
  ElMessageBox.confirm(
      '您确定要将设备[' + scmi.scmiEncode + ']删除吗？',
      'Warning',
      {
        confirmButtonText: '是的',
        cancelButtonText: '点错了',
        type: 'warning',
      }
  ).then(() => {
    scmiRequest.delete(scmi.scmiId, res => {
      ElMessage({
        type: "success",
        message: "操作完成"
      });
      loadScmis();
    })
  }).catch(() => {
  });
}

onMounted(()=>{
  loadScmis();
})
</script>

<style scoped>

</style>